package practice04;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice04
 * @ClassName: UpLoadFile
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/31 17:18
 * @Copyright 大牛版板所有
 */
public class UpLoadFile {
    public static void main(String[] args) {
        File file = getFile();
        Socket client = null;
        try {
            client = new Socket("192.168.0.110",5000);
            DataInputStream iut = new DataInputStream(new FileInputStream(file));
            OutputStream out = client.getOutputStream();
            byte[] bytes = new byte[10240];
            int len = 0;
            while ((len=iut.read(bytes)) != -1){
                out.write(bytes,0,len);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (client != null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static File getFile(){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入你要上传的文件路径：");
        while (true){
            File file = new File(input.next());
            if (!file.exists()){
                System.out.println("你输入的文件不存在！");
            }
            else if (file.isDirectory()){
                System.out.println("你输入的是一个目录！");
            }
            return file;
        }
    }
}
